%     AERO LOAD - программа для нагружения агрегатов самолета распределенной нагрузкой
%     Copyright (C) 2018-2024  Михаил Козлов
%     Сайт проекта: <http://www.fastenerdoctor.ru/>
%     Адрес электронной почты для связи с разработчиком:
%     <doctor.fastener@gmail.com>
% 
%     Эта программа является свободным программным обеспечением: Вы можете
%     распространять ее и/или изменять, соблюдая условия Универсальной общественной
%     лицензии GNU, опубликованной Фондом свободного программного обеспечения,
%     либо редакции 3, либо (на Ваше усмотрение) любой редакции, выпущенной позже.
% 
%     Эта программа распространяется в надежде на то, что она окажется полезной,
%     но БЕЗ КАКИХ-ЛИБО ГАРАНТИЙ, включая даже подразумеваемую гарантию ТОВАРНОЙ ПРИГОДНОСТИ,
%     либо ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Ознакомьтесь с Универсальной общественной
%     лицензией GNU для получения более подробной информации.
% 
%     Вы должны были получить копию Универсальной общественной лицензии GNU
%     вместе с этой программой. Если Вы ее не получили, то перейдите по адресу:
%     <http://www.gnu.org/licenses/>.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <https://www.gnu.org/licenses/>.

function GW_Plot_Erase_Fields(src,~)

global Graphic_Window_Handle;
global Graphics_Properties;
global Graphics;
global DB_ELEMENTS;

global ELEMENT_FORCES;
global ELEMENT_AREAS;
global ELEMENT_NORMAL_PROJECTIONS;

Axes_Handle = findall(Graphic_Window_Handle, 'Tag', 'Main_Axes');

switch src.Tag
    
    case 'Plot_Fields_Button'
        
        % Erase
        tmp.Value = 1;
        tmp.Tag = 'Erase_Fields_Button';
        GW_Plot_Erase_Fields(tmp,[]);
        
        switch Graphics.Load.Distributed_Load_Mode

            case 'CID_DISTR_LOAD'

                % Draw_CID_Distr_Load_Button
                tmp.Value = 1;
                tmp.Tag = 'Draw_CID_Distr_Load_Button';
                GW_Plot_Erase_Fields(tmp,[]);
                set(findall(Graphic_Window_Handle, 'Tag', tmp.Tag), 'Value', 1);

            case 'PRESSURE'

                % Draw_Pressure_Field_Button
                tmp.Value = 1;
                tmp.Tag = 'Draw_Pressure_Field_Button';
                GW_Plot_Erase_Fields(tmp,[]);
                set(findall(Graphic_Window_Handle, 'Tag', tmp.Tag), 'Value', 1);

        end
        
    case 'Erase_Fields_Button'
        
        tmp.Value = 0;
        tmp.Tag = 'Draw_CID_Distr_Load_Button';
        GW_Plot_Erase_Fields(tmp,[]);
        set(findall(Graphic_Window_Handle, 'Tag', tmp.Tag), 'Value', 0);
        
        tmp.Value = 0;
        tmp.Tag = 'Draw_Pressure_Field_Button';
        GW_Plot_Erase_Fields(tmp,[]);
        set(findall(Graphic_Window_Handle, 'Tag', tmp.Tag), 'Value', 0);
        
        tmp.Value = 0;
        tmp.Tag = 'Draw_Pressure_Pn_Pt_Button';
        GW_Plot_Erase_Fields(tmp,[]);
        set(findall(Graphic_Window_Handle, 'Tag', tmp.Tag), 'Value', 0);
        
    case 'Draw_CID_Distr_Load_Button'
        
        if (src.Value == 1)
            
            Button_Group_Field_Type = findall(Graphic_Window_Handle, 'Tag', 'Button_Group_Field_Type');
            Button_Group_Application_Point = findall(Graphic_Window_Handle, 'Tag', 'Button_Group_Application_Point');
            Button_Group_Vectors = findall(Graphic_Window_Handle, 'Tag', 'Button_Group_Vectors');
            
            % Нормированные значения поля
            
            Elm_Field_Values = zeros(size(DB_ELEMENTS,1),1);
            
            switch Button_Group_Field_Type.SelectedObject.Tag
                
                case 'Radio_Button_Forces'
                    
                    Elm_Field_Values = ELEMENT_FORCES;
                    
                case 'Radio_Button_Forces_Areas'
                    
                    Elm_Field_Values = ELEMENT_FORCES ./ ELEMENT_AREAS;
                    
                case 'Radio_Button_Forces_Proj_Areas'
                    
                    for i=1:size(Elm_Field_Values,1)
                        Elm_Field_Values(i) = ELEMENT_FORCES(i) / ELEMENT_AREAS(i) / abs(ELEMENT_NORMAL_PROJECTIONS(i));
                    end
                    
            end
            
            Max_Field_Value = 0;
            
            for i=1:size(Elm_Field_Values,1)
                
                if (abs(Elm_Field_Values(i)) > Max_Field_Value)
                    Max_Field_Value = abs(Elm_Field_Values(i));
                end
                
            end
            
            Elm_Field_Values = Elm_Field_Values / Max_Field_Value;
            
            % Центроиды
            
            Centroids = zeros(size(DB_ELEMENTS,1),3);
            
            switch Button_Group_Application_Point.SelectedObject.Tag
                
                case 'Radio_Button_Mesh'
                    
                    for i=1:size(DB_ELEMENTS,1)
                        Centroids(i,:) = Get_Shell_Centroid(DB_ELEMENTS{i}.ID)';
                    end
                    
                case 'Radio_Button_Proj_Mesh'
                    
                    
                    for i=1:size(DB_ELEMENTS,1)
                        Centroids(i,:) = Project_Point_To_Plane(Get_Shell_Centroid(DB_ELEMENTS{i}.ID), Graphics.Load.Basic_Plane.Equation)';
                    end
                    
            end
            
            % Начальные и конечные точки
            
            Start_Points = zeros(size(DB_ELEMENTS,1),3);
            End_Points = zeros(size(DB_ELEMENTS,1),3);
            
            switch Button_Group_Vectors.SelectedObject.Tag
                
                case 'Radio_Button_From_Point'
                    
                    Start_Points = Centroids;
                    
                    for i=1:size(End_Points, 1)
                        
                        End_Points(i,:) = Start_Points(i,:) + (Graphics.Load.Vector * Elm_Field_Values(i) * Graphics.CID_Distr_Load_Vector_Length)';
                        
                    end
                    
                case 'Radio_Button_To_Point'
                    
                    End_Points = Centroids;
                    
                    for i=1:size(Start_Points, 1)
                        
                        Start_Points(i,:) = End_Points(i,:) - (Graphics.Load.Vector * Elm_Field_Values(i) * Graphics.CID_Distr_Load_Vector_Length)';
                        
                    end
                    
            end
            
            % Массив для построения
            
            Tmp = nan(11*size(DB_ELEMENTS,1), 3);
            
            for i=1:size(DB_ELEMENTS,1)
                
                Normal_vector = Graphics.Load.Vector;
                
                Tmp_Vector_1 = Graphics.Load.Chord_Surface.A1_projected - Graphics.Load.Chord_Surface.A2_projected;
                Tmp_Vector_1 = Tmp_Vector_1/norm(Tmp_Vector_1);

                Tmp_Vector_2 = cross(Tmp_Vector_1, Normal_vector);
                if (norm(Tmp_Vector_2) < 1e-6)
                    Tmp_Vector_2 = cross(Graphics.Load.Vector, Normal_vector);
                end
                Tmp_Vector_2 = Tmp_Vector_2/norm(Tmp_Vector_2);
                
                Tmp_Vector_1 = cross(Normal_vector, Tmp_Vector_2);
                Tmp_Vector_1 = Tmp_Vector_1/norm(Tmp_Vector_1);
                
                Tmp(11*i-10,:) = Start_Points(i,:);
                Tmp(11*i-9,:) = End_Points(i,:);
                Tmp(11*i-8,:) = Tmp(11*i-9,:) - (Graphics_Properties.CID_Distr_Load_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.CID_Distr_Load_Vector_Length * Tmp_Vector_1)' - (Graphics.Load.Vector * Elm_Field_Values(i) * Graphics.CID_Distr_Load_Vector_Length * Graphics_Properties.CID_Distr_Load_Arrow_Size_1)';
                Tmp(11*i-7,:) = Tmp(11*i-8,:) + (Graphics_Properties.CID_Distr_Load_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.CID_Distr_Load_Vector_Length * Tmp_Vector_1)';
                Tmp(11*i-6,:) = Tmp(11*i-7,:) + (Graphics_Properties.CID_Distr_Load_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.CID_Distr_Load_Vector_Length * Tmp_Vector_2)';
                Tmp(11*i-5,:) = Tmp(11*i-6,:) - (Graphics_Properties.CID_Distr_Load_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.CID_Distr_Load_Vector_Length * Tmp_Vector_2)' + (Graphics.Load.Vector * Elm_Field_Values(i) * Graphics.CID_Distr_Load_Vector_Length * Graphics_Properties.CID_Distr_Load_Arrow_Size_1)';
                Tmp(11*i-4,:) = Tmp(11*i-5,:) + (Graphics_Properties.CID_Distr_Load_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.CID_Distr_Load_Vector_Length * Tmp_Vector_1)' - (Graphics.Load.Vector * Elm_Field_Values(i) * Graphics.CID_Distr_Load_Vector_Length * Graphics_Properties.CID_Distr_Load_Arrow_Size_1)';
                Tmp(11*i-3,:) = Tmp(11*i-4,:) - (Graphics_Properties.CID_Distr_Load_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.CID_Distr_Load_Vector_Length * Tmp_Vector_1)';
                Tmp(11*i-2,:) = Tmp(11*i-3,:) - (Graphics_Properties.CID_Distr_Load_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.CID_Distr_Load_Vector_Length * Tmp_Vector_2)';
                Tmp(11*i-1,:) = Tmp(11*i-9,:);
                
            end

            % Построение
            
            Vector_Plot_Handle = plot3(Axes_Handle, Tmp(:,1), Tmp(:,2), Tmp(:,3));

            set(Vector_Plot_Handle, 'Color', Graphics_Properties.CID_Distr_Load_Vector_Color);
            set(Vector_Plot_Handle, 'LineWidth', Graphics_Properties.Distr_Load_Vector_Line_Width);
            set(Vector_Plot_Handle, 'Tag', 'CID_Distr_Load');
            
            % Включение кнопок
            
            set(Button_Group_Field_Type, 'ForegroundColor', uint8([0 0 0]));
            set(Button_Group_Application_Point, 'ForegroundColor', uint8([0 0 0]));
            set(Button_Group_Vectors, 'ForegroundColor', uint8([0 0 0]));
            
            Radio_Button_Forces = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_Forces');
            set(Radio_Button_Forces, 'Enable', 'on');
            
            Radio_Button_Forces_Areas = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_Forces_Areas');
            set(Radio_Button_Forces_Areas, 'Enable', 'on');
            
            Radio_Button_Forces_Proj_Areas = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_Forces_Proj_Areas');
            set(Radio_Button_Forces_Proj_Areas, 'Enable', 'on');
            
            Radio_Button_Mesh = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_Mesh');
            set(Radio_Button_Mesh, 'Enable', 'on');
            
            Radio_Button_Proj_Mesh = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_Proj_Mesh');
            set(Radio_Button_Proj_Mesh, 'Enable', 'on');
            
            Radio_Button_From_Point = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_From_Point');
            set(Radio_Button_From_Point, 'Enable', 'on');
            
            Radio_Button_To_Point = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_To_Point');
            set(Radio_Button_To_Point, 'Enable', 'on');
            
        else
            
            delete(findall(Graphic_Window_Handle, 'Tag', 'CID_Distr_Load'));
            
            Button_Group_Field_Type = findall(Graphic_Window_Handle, 'Tag', 'Button_Group_Field_Type');
            set(Button_Group_Field_Type, 'ForegroundColor', uint8([109 109 109]));
            
            Button_Group_Application_Point = findall(Graphic_Window_Handle, 'Tag', 'Button_Group_Application_Point');
            set(Button_Group_Application_Point, 'ForegroundColor', uint8([109 109 109]));
            
            Button_Group_Vectors = findall(Graphic_Window_Handle, 'Tag', 'Button_Group_Vectors');
            set(Button_Group_Vectors, 'ForegroundColor', uint8([109 109 109]));
            
            Radio_Button_Forces = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_Forces');
            set(Radio_Button_Forces, 'Enable', 'off');
            
            Radio_Button_Forces_Areas = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_Forces_Areas');
            set(Radio_Button_Forces_Areas, 'Enable', 'off');
            
            Radio_Button_Forces_Proj_Areas = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_Forces_Proj_Areas');
            set(Radio_Button_Forces_Proj_Areas, 'Enable', 'off');
            
            Radio_Button_Mesh = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_Mesh');
            set(Radio_Button_Mesh, 'Enable', 'off');
            
            Radio_Button_Proj_Mesh = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_Proj_Mesh');
            set(Radio_Button_Proj_Mesh, 'Enable', 'off');
            
            Radio_Button_From_Point = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_From_Point');
            set(Radio_Button_From_Point, 'Enable', 'off');
            
            Radio_Button_To_Point = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_To_Point');
            set(Radio_Button_To_Point, 'Enable', 'off');
            
        end
        
    case {'Button_Group_Field_Type', 'Button_Group_Application_Point', 'Button_Group_Vectors'}
        
        tmp.Value = 0;
        tmp.Tag = 'Draw_CID_Distr_Load_Button';
        GW_Plot_Erase_Fields(tmp,[]);
        set(findall(Graphic_Window_Handle, 'Tag', tmp.Tag), 'Value', 0);
        
        tmp.Value = 1;
        tmp.Tag = 'Draw_CID_Distr_Load_Button';
        GW_Plot_Erase_Fields(tmp,[]);
        set(findall(Graphic_Window_Handle, 'Tag', tmp.Tag), 'Value', 1);
        
    case 'Draw_Pressure_Field_Button'
        
        if (src.Value == 1)
            
            Button_Group_Pressure_Vectors = findall(Graphic_Window_Handle, 'Tag', 'Button_Group_Pressure_Vectors');
            
            % Нормированные значения поля
            
            Elm_Field_Values = zeros(size(DB_ELEMENTS,1),1);
            
            for i=1:size(Elm_Field_Values,1)
                Elm_Field_Values(i) = ELEMENT_FORCES(i) / ELEMENT_AREAS(i) / ELEMENT_NORMAL_PROJECTIONS(i);
            end
            
            Max_Field_Value = 0;
            
            for i=1:size(Elm_Field_Values,1)
                
                if (abs(Elm_Field_Values(i)) > Max_Field_Value)
                    Max_Field_Value = abs(Elm_Field_Values(i));
                end
                
            end
            
            Elm_Field_Values = Elm_Field_Values / Max_Field_Value;
            
            % Центроиды
            
            Centroids = zeros(size(DB_ELEMENTS,1),3);
            
            for i=1:size(DB_ELEMENTS,1)
                Centroids(i,:) = Get_Shell_Centroid(DB_ELEMENTS{i}.ID)';
            end
            
            % Начальные и конечные точки
            
            Start_Points = zeros(size(DB_ELEMENTS,1),3);
            End_Points = zeros(size(DB_ELEMENTS,1),3);
            
            switch Button_Group_Pressure_Vectors.SelectedObject.Tag
                
                case 'Radio_Button_From_Point_Pressure'
                    
                    Start_Points = Centroids;
                    
                    for i=1:size(End_Points, 1)
                        
                        Normal_vector = Get_Shell_Normal(DB_ELEMENTS{i}.ID);
                        End_Points(i,:) = Start_Points(i,:) + (Normal_vector * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length)';
                        
                    end
                    
                case 'Radio_Button_To_Point_Pressure'
                    
                    End_Points = Centroids;
                    
                    for i=1:size(Start_Points, 1)
                        
                        Normal_vector = Get_Shell_Normal(DB_ELEMENTS{i}.ID);
                        Start_Points(i,:) = End_Points(i,:) - (Normal_vector * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length)';
                        
                    end
                    
            end
            
            % Массив для построения
            
            Tmp = nan(11*size(DB_ELEMENTS,1), 3);
            
            for i=1:size(DB_ELEMENTS,1)
                
                Normal_vector = Get_Shell_Normal(DB_ELEMENTS{i}.ID);
                
                Tmp_Vector_1 = Graphics.Load.Chord_Surface.A1_projected - Graphics.Load.Chord_Surface.A2_projected;
                Tmp_Vector_1 = Tmp_Vector_1/norm(Tmp_Vector_1);

                Tmp_Vector_2 = cross(Tmp_Vector_1, Normal_vector);
                if (norm(Tmp_Vector_2) < 1e-6)
                    Tmp_Vector_2 = cross(Graphics.Load.Vector, Normal_vector);
                end
                Tmp_Vector_2 = Tmp_Vector_2/norm(Tmp_Vector_2);
                
                Tmp_Vector_1 = cross(Normal_vector, Tmp_Vector_2);
                Tmp_Vector_1 = Tmp_Vector_1/norm(Tmp_Vector_1);
                
                Tmp(11*i-10,:) = Start_Points(i,:);
                Tmp(11*i-9,:) = End_Points(i,:);
                Tmp(11*i-8,:) = Tmp(11*i-9,:) - (Graphics_Properties.Pressure_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length * Tmp_Vector_1)' - (Normal_vector * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length * Graphics_Properties.Pressure_Arrow_Size_1)';
                Tmp(11*i-7,:) = Tmp(11*i-8,:) + (Graphics_Properties.Pressure_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length * Tmp_Vector_1)';
                Tmp(11*i-6,:) = Tmp(11*i-7,:) + (Graphics_Properties.Pressure_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length * Tmp_Vector_2)';
                Tmp(11*i-5,:) = Tmp(11*i-6,:) - (Graphics_Properties.Pressure_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length * Tmp_Vector_2)' + (Normal_vector * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length * Graphics_Properties.Pressure_Arrow_Size_1)';
                Tmp(11*i-4,:) = Tmp(11*i-5,:) + (Graphics_Properties.Pressure_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length * Tmp_Vector_1)' - (Normal_vector * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length * Graphics_Properties.Pressure_Arrow_Size_1)';
                Tmp(11*i-3,:) = Tmp(11*i-4,:) - (Graphics_Properties.Pressure_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length * Tmp_Vector_1)';
                Tmp(11*i-2,:) = Tmp(11*i-3,:) - (Graphics_Properties.Pressure_Arrow_Size_2 * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length * Tmp_Vector_2)';
                Tmp(11*i-1,:) = Tmp(11*i-9,:);
                
            end

            % Построение
            
            Vector_Plot_Handle = plot3(Axes_Handle, Tmp(:,1), Tmp(:,2), Tmp(:,3));

            set(Vector_Plot_Handle, 'Color', Graphics_Properties.Pressure_Vector_Color);
            set(Vector_Plot_Handle, 'LineWidth', Graphics_Properties.Pressure_Vector_Line_Width);
            set(Vector_Plot_Handle, 'Tag', 'Pressure_Field');

            % Кнопки
            
            tmp.Value = 0;
            tmp.Tag = 'Draw_Pressure_Pn_Pt_Button';
            GW_Plot_Erase_Fields(tmp,[]);
            set(findall(Graphic_Window_Handle, 'Tag', tmp.Tag), 'Value', 0);
            
            set(Button_Group_Pressure_Vectors, 'ForegroundColor', uint8([0 0 0]));
            
            Radio_Button_From_Point_Pressure = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_From_Point_Pressure');
            set(Radio_Button_From_Point_Pressure, 'Enable', 'on');
            
            Radio_Button_To_Point_Pressure = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_To_Point_Pressure');
            set(Radio_Button_To_Point_Pressure, 'Enable', 'on');
            
            
        else
            
            delete(findall(Graphic_Window_Handle, 'Tag', 'Pressure_Field'));
            
            Button_Group_Pressure_Vectors = findall(Graphic_Window_Handle, 'Tag', 'Button_Group_Pressure_Vectors');
            set(Button_Group_Pressure_Vectors, 'ForegroundColor', uint8([109 109 109]));
            
            Radio_Button_From_Point_Pressure = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_From_Point_Pressure');
            set(Radio_Button_From_Point_Pressure, 'Enable', 'off');
            
            Radio_Button_To_Point_Pressure = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_To_Point_Pressure');
            set(Radio_Button_To_Point_Pressure, 'Enable', 'off');
            
        end
        
    case 'Draw_Pressure_Pn_Pt_Button'
        
        if (src.Value == 1)
            
            Button_Group_Pressure_Vectors = findall(Graphic_Window_Handle, 'Tag', 'Button_Group_Pressure_Vectors');
            
            % Нормированные значения поля
            
            Elm_Field_Values = zeros(size(DB_ELEMENTS,1),1);
            
            for i=1:size(Elm_Field_Values,1)
                Elm_Field_Values(i) = ELEMENT_FORCES(i) / ELEMENT_AREAS(i) / ELEMENT_NORMAL_PROJECTIONS(i);
            end
            
            Max_Field_Value = 0;
            
            for i=1:size(Elm_Field_Values,1)
                
                if (abs(Elm_Field_Values(i)) > Max_Field_Value)
                    Max_Field_Value = abs(Elm_Field_Values(i));
                end
                
            end
            
%             for i=1:size(Elm_Field_Values,1)
%                 
%                 length_1 = abs(ELEMENT_NORMAL_PROJECTIONS(i) * Elm_Field_Values(i));
%                 length_2 = sqrt(Elm_Field_Values(i)^2 - length_1^2);
%                 
%                 if (length_1 > Max_Field_Value)
%                     Max_Field_Value = length_1;
%                 end
%                 
%                 if (length_2 > Max_Field_Value)
%                     Max_Field_Value = length_2;
%                 end
%                 
%             end
            
            Elm_Field_Values = Elm_Field_Values / Max_Field_Value;
            
            % Центроиды
            
            Centroids = zeros(size(DB_ELEMENTS,1),3);
            
            for i=1:size(DB_ELEMENTS,1)
                Centroids(i,:) = Get_Shell_Centroid(DB_ELEMENTS{i}.ID)';
            end
            
            % Начальные и конечные точки
            
            Start_Points_Norm = zeros(size(DB_ELEMENTS,1),3);
            Start_Points_Tang = zeros(size(DB_ELEMENTS,1),3);
            
            End_Points_Norm = zeros(size(DB_ELEMENTS,1),3);
            End_Points_Tang = zeros(size(DB_ELEMENTS,1),3);
            
            switch Button_Group_Pressure_Vectors.SelectedObject.Tag
                
                case 'Radio_Button_From_Point_Pressure'
                    
                    Start_Points_Norm = Centroids;
                    Start_Points_Tang = Centroids;
                    
                    for i=1:size(DB_ELEMENTS,1)
                        
                        Normal_vector = Get_Shell_Normal(DB_ELEMENTS{i}.ID);
                        Pressure_vector = Normal_vector * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length;
                        
                        Pressure_Normal_vector = dot(Pressure_vector, Graphics.Load.Vector) * Graphics.Load.Vector;
                        Pressure_Tang_vector = Pressure_vector - Pressure_Normal_vector;
                        
                        End_Points_Norm(i,:) = Start_Points_Norm(i,:) + Pressure_Normal_vector';
                        End_Points_Tang(i,:) = Start_Points_Tang(i,:) + Pressure_Tang_vector';
                        
                    end
                    
                case 'Radio_Button_To_Point_Pressure'
                    
                    End_Points_Norm = Centroids;
                    End_Points_Tang = Centroids;
                    
                    for i=1:size(DB_ELEMENTS,1)
                        
                        Normal_vector = Get_Shell_Normal(DB_ELEMENTS{i}.ID);
                        Pressure_vector = Normal_vector * Elm_Field_Values(i) * Graphics.Pressure_Vector_Length;
                        
                        Pressure_Normal_vector = dot(Pressure_vector, Graphics.Load.Vector) * Graphics.Load.Vector;
                        Pressure_Tang_vector = Pressure_vector - Pressure_Normal_vector;
                        
                        Start_Points_Norm(i,:) = End_Points_Norm(i,:) - Pressure_Normal_vector';
                        Start_Points_Tang(i,:) = End_Points_Tang(i,:) - Pressure_Tang_vector';
                        
                    end
                    
            end
            
            % Массив для построения нормальных компонент
            
            Tmp_Norm = nan(11*size(DB_ELEMENTS,1), 3);
            
            for i=1:size(DB_ELEMENTS,1)
                
                Vector = (End_Points_Norm(i,:) - Start_Points_Norm(i,:))';
                Vector_norm = norm(Vector);
                
                Tmp_Vector_1 = Graphics.Load.Chord_Surface.A1_projected - Graphics.Load.Chord_Surface.A2_projected;
                Tmp_Vector_1 = Tmp_Vector_1/norm(Tmp_Vector_1);

                Tmp_Vector_2 = cross(Tmp_Vector_1, Vector/Vector_norm);
                if (norm(Tmp_Vector_2) < 1e-6)
                    Tmp_Vector_2 = cross(Graphics.Load.Vector, Vector/Vector_norm);
                end
                Tmp_Vector_2 = Tmp_Vector_2/norm(Tmp_Vector_2);
                
                Tmp_Vector_1 = cross(Vector/Vector_norm, Tmp_Vector_2);
                Tmp_Vector_1 = Tmp_Vector_1/norm(Tmp_Vector_1);
                
                Tmp_Norm(11*i-10,:) = Start_Points_Norm(i,:);
                Tmp_Norm(11*i-9,:) = End_Points_Norm(i,:);
                Tmp_Norm(11*i-8,:) = Tmp_Norm(11*i-9,:) - (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_1)' - (Vector * Graphics_Properties.Pressure_Arrow_Size_1)';
                Tmp_Norm(11*i-7,:) = Tmp_Norm(11*i-8,:) + (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_1)';
                Tmp_Norm(11*i-6,:) = Tmp_Norm(11*i-7,:) + (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_2)';
                Tmp_Norm(11*i-5,:) = Tmp_Norm(11*i-6,:) - (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_2)' + (Vector * Graphics_Properties.Pressure_Arrow_Size_1)';
                Tmp_Norm(11*i-4,:) = Tmp_Norm(11*i-5,:) + (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_1)' - (Vector * Graphics_Properties.Pressure_Arrow_Size_1)';
                Tmp_Norm(11*i-3,:) = Tmp_Norm(11*i-4,:) - (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_1)';
                Tmp_Norm(11*i-2,:) = Tmp_Norm(11*i-3,:) - (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_2)';
                Tmp_Norm(11*i-1,:) = Tmp_Norm(11*i-9,:);
                
            end
            
            % Массив для построения тангенциальных компонент
            
            Tmp_Tang = nan(11*size(DB_ELEMENTS,1), 3);
            
            for i=1:size(DB_ELEMENTS,1)
                
                Vector = (End_Points_Tang(i,:) - Start_Points_Tang(i,:))';
                Vector_norm = norm(Vector);
                
                Tmp_Vector_1 = Graphics.Load.Chord_Surface.A1_projected - Graphics.Load.Chord_Surface.A2_projected;
                Tmp_Vector_1 = Tmp_Vector_1/norm(Tmp_Vector_1);

                Tmp_Vector_2 = cross(Tmp_Vector_1, Vector/Vector_norm);
                if (norm(Tmp_Vector_2) < 1e-6)
                    Tmp_Vector_2 = cross(Graphics.Load.Vector, Vector/Vector_norm);
                end
                Tmp_Vector_2 = Tmp_Vector_2/norm(Tmp_Vector_2);
                
                Tmp_Vector_1 = cross(Vector/Vector_norm, Tmp_Vector_2);
                Tmp_Vector_1 = Tmp_Vector_1/norm(Tmp_Vector_1);
                
                Tmp_Tang(11*i-10,:) = Start_Points_Tang(i,:);
                Tmp_Tang(11*i-9,:) = End_Points_Tang(i,:);
                Tmp_Tang(11*i-8,:) = Tmp_Tang(11*i-9,:) - (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_1)' - (Vector * Graphics_Properties.Pressure_Arrow_Size_1)';
                Tmp_Tang(11*i-7,:) = Tmp_Tang(11*i-8,:) + (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_1)';
                Tmp_Tang(11*i-6,:) = Tmp_Tang(11*i-7,:) + (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_2)';
                Tmp_Tang(11*i-5,:) = Tmp_Tang(11*i-6,:) - (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_2)' + (Vector * Graphics_Properties.Pressure_Arrow_Size_1)';
                Tmp_Tang(11*i-4,:) = Tmp_Tang(11*i-5,:) + (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_1)' - (Vector * Graphics_Properties.Pressure_Arrow_Size_1)';
                Tmp_Tang(11*i-3,:) = Tmp_Tang(11*i-4,:) - (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_1)';
                Tmp_Tang(11*i-2,:) = Tmp_Tang(11*i-3,:) - (Graphics_Properties.Pressure_Arrow_Size_2 * Vector_norm * Tmp_Vector_2)';
                Tmp_Tang(11*i-1,:) = Tmp_Tang(11*i-9,:);
                
            end
            
            % Построение
            
            Vector_Plot_Handle = plot3(Axes_Handle, Tmp_Norm(:,1), Tmp_Norm(:,2), Tmp_Norm(:,3));

            set(Vector_Plot_Handle, 'Color', Graphics_Properties.Normal_Pressure_Vector_Color);
            set(Vector_Plot_Handle, 'LineWidth', Graphics_Properties.Pressure_Vector_Line_Width);
            set(Vector_Plot_Handle, 'Tag', 'Pressure_Field_Norm');
            
            Vector_Plot_Handle = plot3(Axes_Handle, Tmp_Tang(:,1), Tmp_Tang(:,2), Tmp_Tang(:,3));

            set(Vector_Plot_Handle, 'Color', Graphics_Properties.Tangential_Pressure_Vector_Color);
            set(Vector_Plot_Handle, 'LineWidth', Graphics_Properties.Pressure_Vector_Line_Width);
            set(Vector_Plot_Handle, 'Tag', 'Pressure_Field_Tang');
            
            % Кнопки
            
            tmp.Value = 0;
            tmp.Tag = 'Draw_Pressure_Field_Button';
            GW_Plot_Erase_Fields(tmp,[]);
            set(findall(Graphic_Window_Handle, 'Tag', tmp.Tag), 'Value', 0);
            
            set(Button_Group_Pressure_Vectors, 'ForegroundColor', uint8([0 0 0]));
            
            Radio_Button_From_Point_Pressure = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_From_Point_Pressure');
            set(Radio_Button_From_Point_Pressure, 'Enable', 'on');
            
            Radio_Button_To_Point_Pressure = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_To_Point_Pressure');
            set(Radio_Button_To_Point_Pressure, 'Enable', 'on');
            
        else
            
            delete(findall(Graphic_Window_Handle, 'Tag', 'Pressure_Field_Norm'));
            delete(findall(Graphic_Window_Handle, 'Tag', 'Pressure_Field_Tang'));
            
            Button_Group_Pressure_Vectors = findall(Graphic_Window_Handle, 'Tag', 'Button_Group_Pressure_Vectors');
            set(Button_Group_Pressure_Vectors, 'ForegroundColor', uint8([109 109 109]));
            
            Radio_Button_From_Point_Pressure = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_From_Point_Pressure');
            set(Radio_Button_From_Point_Pressure, 'Enable', 'off');
            
            Radio_Button_To_Point_Pressure = findall(Graphic_Window_Handle, 'Tag', 'Radio_Button_To_Point_Pressure');
            set(Radio_Button_To_Point_Pressure, 'Enable', 'off');
            
        end
        
    case 'Button_Group_Pressure_Vectors'
        
        Draw_Pressure_Field_Button = findall(Graphic_Window_Handle, 'Tag', 'Draw_Pressure_Field_Button');
        
        if (Draw_Pressure_Field_Button.Value == 1)
            
            tmp.Value = 0;
            tmp.Tag = 'Draw_Pressure_Field_Button';
            GW_Plot_Erase_Fields(tmp,[]);
            set(findall(Graphic_Window_Handle, 'Tag', tmp.Tag), 'Value', 0);

            tmp.Value = 1;
            tmp.Tag = 'Draw_Pressure_Field_Button';
            GW_Plot_Erase_Fields(tmp,[]);
            set(findall(Graphic_Window_Handle, 'Tag', tmp.Tag), 'Value', 1);
            
        end
        
        Draw_Pressure_Pn_Pt_Button = findall(Graphic_Window_Handle, 'Tag', 'Draw_Pressure_Pn_Pt_Button');
        
        if (Draw_Pressure_Pn_Pt_Button.Value == 1)
            
            tmp.Value = 0;
            tmp.Tag = 'Draw_Pressure_Pn_Pt_Button';
            GW_Plot_Erase_Fields(tmp,[]);
            set(findall(Graphic_Window_Handle, 'Tag', tmp.Tag), 'Value', 0);

            tmp.Value = 1;
            tmp.Tag = 'Draw_Pressure_Pn_Pt_Button';
            GW_Plot_Erase_Fields(tmp,[]);
            set(findall(Graphic_Window_Handle, 'Tag', tmp.Tag), 'Value', 1);
            
        end
        
end
